/**
 * First Panel in Create Pivot Table Wizard. Offers users choice to do the overall subject screen or conditional subject
 * screen
 */
package slhs.tempo.pivotTableUtilities;

import java.awt.LayoutManager;
import java.awt.event.ItemListener;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JRadioButton;

import org.mlc.swing.layout.LayoutConstraintsManager;
import org.openswing.swing.wizard.client.WizardInnerPanel;
import org.openswing.swing.wizard.client.WizardPanel;

/**
 * CustomFirstPanel.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class PivotWizardFirstPanel extends WizardInnerPanel {

    private static final String FIRST_PANEL_LAYOUT_XML_FILE = "FIRST_PANEL_LAYOUT.XML";
    public static final String OVERALL_ANALYSIS_ACROSS_SUBJECTS_ACROSS_CONDITIONS = "Overall Analysis Across Subjects Across Conditions";
    public static final String OVERALL_ANALYSIS_PER_SUBJECT_ACROSS_CONDITIONS = "Overall Analysis Per Subject Across Conditions";
    public static final String CONDITIONAL_ANALYSIS_PER_SUBJECT_PER_CONDITION = "Conditional Analysis";

    public static final String SCREEN_ID = "FIRST";
    private JRadioButton conditionalAnalysisPerSubjectPerConditionOptionRadioButton;
    private JRadioButton overallAnalysisPerSubjectPerConditionOptionRadioButton;
    private JRadioButton overallAnalysisAcrossSubjectsAcrossConditionsOptionRadioButton;

    private ButtonGroup buttonGroup;
    private JLabel operationSelectLabel;

    private final WizardPanel wizardPanelObject;

    public PivotWizardFirstPanel(WizardPanel wizardPanel) {
	wizardPanelObject = wizardPanel;
	initComponents();
	addComponents();
    }

    /**
     * Initialize all the required components
     */
    private void initComponents() {
	conditionalAnalysisPerSubjectPerConditionOptionRadioButton = new JRadioButton(
		"Conditional Analysis Per Subject Per Condition");
	operationSelectLabel = new JLabel("Select an operation to perform");
	overallAnalysisPerSubjectPerConditionOptionRadioButton = new JRadioButton(
		"Overall Analysis Per Subject Across Conditions");
	overallAnalysisAcrossSubjectsAcrossConditionsOptionRadioButton = new JRadioButton(
		"Overall Analysis Across Subjects Across Conditions");
	buttonGroup = new ButtonGroup();
    }

    /**
     * Add all the required components
     */
    private void addComponents() {
	buttonGroup.add(overallAnalysisPerSubjectPerConditionOptionRadioButton);
	buttonGroup.add(conditionalAnalysisPerSubjectPerConditionOptionRadioButton);
	buttonGroup.add(overallAnalysisAcrossSubjectsAcrossConditionsOptionRadioButton);

	LayoutConstraintsManager layoutConstraintsManager = LayoutConstraintsManager.getLayoutConstraintsManager(this
		.getClass().getResourceAsStream(FIRST_PANEL_LAYOUT_XML_FILE));
	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	this.setLayout(panelLayout);

	// here we add the controls to the container.
	this.add(conditionalAnalysisPerSubjectPerConditionOptionRadioButton,
		"conditionalAnalysisPerSubjectPerConditionOption");
	this.add(operationSelectLabel, "operationSelectLabel");
	this.add(overallAnalysisPerSubjectPerConditionOptionRadioButton, "overallAnalysisPerSubjectPerConditionOption");
	this.add(overallAnalysisAcrossSubjectsAcrossConditionsOptionRadioButton,
		"overallAnalysisAcrossSubjectsAcrossConditionsOptionRadioButton");

	// configure components
	operationSelectLabel.setFont(new java.awt.Font("Dialog", 1, 12));
	conditionalAnalysisPerSubjectPerConditionOptionRadioButton
		.setName(CONDITIONAL_ANALYSIS_PER_SUBJECT_PER_CONDITION);
	overallAnalysisPerSubjectPerConditionOptionRadioButton.setName(OVERALL_ANALYSIS_PER_SUBJECT_ACROSS_CONDITIONS);
	overallAnalysisAcrossSubjectsAcrossConditionsOptionRadioButton
		.setName(OVERALL_ANALYSIS_ACROSS_SUBJECTS_ACROSS_CONDITIONS);

    }

    @Override
    public String getPanelId() {
	return SCREEN_ID;
    }

    /**
     * This method is automatically called by WizardPanel when the panel is showed: it can be overridden to add custom
     * logic that must be executed when the panel is showed.
     */
    @Override
    public void init() {
	/**
	 * Everytime a new panel is added to the wizard frame, button states are reset to work around this issue for the
	 * first panel, related buttons are explicitly disabled here
	 * */
	if (buttonGroup.getSelection() == null) {
	    wizardPanelObject.getNextButton().setEnabled(false);
	}

	wizardPanelObject.getBackButton().setEnabled(false);
    }

    /**
     * This method could be overridden.
     * 
     * @return image name; null if no image is required
     */
    @Override
    public String getImageName() {
	return "setup2.gif";
    }

    /**
     * Add an ItemListener to the "ok" radio button.
     * 
     * @param listener
     *            ItemListener added to the "ok" radio button
     */
    public final void addNextRadioButtonItemListener(ItemListener listener) {
	conditionalAnalysisPerSubjectPerConditionOptionRadioButton.addItemListener(listener);
	overallAnalysisPerSubjectPerConditionOptionRadioButton.addItemListener(listener);
	overallAnalysisAcrossSubjectsAcrossConditionsOptionRadioButton.addItemListener(listener);

    }

    /**
     * @return
     */
    public boolean isOverallAnalysisPerSubjectAcrossConditionsSelected() {

	return overallAnalysisPerSubjectPerConditionOptionRadioButton.isSelected();
    }

}
